package com.xiaomei.manager.web.dto;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.ValueFilter;
import com.xiaomei.manager.utils.ReturnCode;



public class RespBody {
	
	// respBody对象中data属性的类型
	public static final Integer RESPBODY_DATA_TYPE_OBJECT = 0; // object类型
	public static final Integer RESPBODY_DATA_TYPE_ARRAY = 1;  // 集合类型
	public static final Integer RESPBODY_DATA_TYPE_OTHER = 2;  // 基础数据类型
	
	public RespBody() {}
	
	public RespBody(String return_code) {
		this.return_code = return_code;
	}
	
	public RespBody(String return_code, Integer data_type, Object data) {
		this.return_code = return_code == null ? this.return_code : return_code;
		this.data = data == null ? this.data : data;
		this.data_type = data_type == null ? this.data_type : data_type;
	}

	// data类型
	private Integer data_type = RESPBODY_DATA_TYPE_OBJECT;
	
	// 返回的操作状态code
	private String return_code = ReturnCode.CODE_0;
	
	// 返回的数据结果
	private Object data = new Object();

	public String getReturn_code() {
		return return_code;
	}

	public void setReturn_code(String return_code) {
		this.return_code = return_code;
	}

//	public String getReturn_msg() {
//		return return_msg;
//	}
//
//	public void setReturn_msg(String return_msg) {
//		this.return_msg = return_msg;
//	}

	public Object getData() {
		if(this.data_type == RESPBODY_DATA_TYPE_OBJECT) {
			return JSONObject.parseObject(JSONObject.toJSONString(data, filter));
		} else if(this.data_type == RESPBODY_DATA_TYPE_ARRAY) {
			return JSONObject.parseArray(JSONObject.toJSONString(data, filter));
		}
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}
	
	private static ValueFilter filter = new ValueFilter() {
        @Override
        public Object process(Object obj, String s, Object v) {
            if (v == null)
                return "";
            return v;
        }
    };
}
